import { HttpContext } from '@angular/common/http';
import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import {STChange, STColumn, STComponent, STPage, STRes} from '@delon/abc/st';
import { XlsxService } from '@delon/abc/xlsx';
import { ALLOW_ANONYMOUS } from '@delon/auth';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';

import { Api } from '../../../shared/api/api';
import {
  DEPARTMENT_LEVEL_STATUS_BADGE,
  ROLE_LEVEL_STATUS_BADGE,
  USER_LABEL1_TAG,
  USER_LABEL_ENUM,
  USER_STATUS_BADGE
} from '../../../shared/constant/common-constant';
import { ResponseCode } from '../../../shared/response.code';
import { BookEditComponent } from './user-list-edit-book.component';

@Component({
  selector: 'components-user-book',
  templateUrl: './user-list-book.component.html'
})
export class ComponentsUserListBookComponent implements OnInit {
  data: any;
  url = Api.BaseUserListApi;
  params: any = {};
  list: any = {
    Sheet1: []
  };
  page: any = {
    data: [],
    current: 1,
    total: 1,
    size: 10
  };
  pagination: STPage = {
    front: false,
    pageSizes: [10, 20, 30, 40, 50],
    total: true,
    showSize: true,
    showQuickJumper: true
  };
  userList: any = [];
  customer: any = {
    name: '',
    company_name: '',
    company_address: '',
    phone: '',
    business_scope: '',
    business_style: ''
  };

  @ViewChild('st', { static: true }) st!: STComponent;
  columns: STColumn[] = [
    { title: '编号', width: 50, index: 'id' },
    { title: '姓名', index: 'user_name' },
    { title: '登录账号', index: 'account' },
    { title: '角色', index: 'sys_role_id' , type: 'badge', badge: ROLE_LEVEL_STATUS_BADGE },
    { title: '级别', index: 'department_level_id', type: 'badge', badge: DEPARTMENT_LEVEL_STATUS_BADGE },
    { title: '手机号', index: 'phone' },
    { title: '地址', index: 'address' },
    { title: '状态', index: 'enable', type: 'badge', badge: USER_STATUS_BADGE },
    { title: '创建时间', width: 120, index: 'creat_time', type: 'date' }
  ];
  customerList: any;

  constructor(
    private xlsx: XlsxService,
    protected http: _HttpClient,
    private modal: ModalHelper,
    private msgSrv: NzMessageService,
    private cdr: ChangeDetectorRef
  ) {}

  ngOnInit(): void {
  }

  queryUserList() {
    const current: number = this.params.current || 1;
    const size: number = this.params.size || 10;
    this.http
      .get(
        Api.BaseUserListApi,
        {
          current: current,
          size: size
        },
        { context: new HttpContext().set(ALLOW_ANONYMOUS, true) }
      )
      .subscribe((res: any) => {
        if (res && res.code === ResponseCode.SUCCESS) {
          if (res.data) this.page = res.data;
        }
      });
  }

  search(event: any) {
    if (event) {
    }
  }

  add() {
    this.modal.createStatic(BookEditComponent).subscribe(() => this.st.reload());
  }
  change(e: STChange): void {
    console.log(e);
  }
}
